package com.czlw.module_activity.viewmodel

import androidx.databinding.ObservableField
import com.czl.base.base.BaseBean
import com.czl.base.base.BaseViewModel
import com.czl.base.base.MyApplication
import com.czl.base.binding.command.BindingAction
import com.czl.base.binding.command.BindingCommand
import com.czl.base.data.DataRepository
import com.czl.base.data.bean.ActivityDetailBean
import com.czl.base.event.SingleLiveEvent
import com.czl.base.extension.ApiSubscriber200Helper
import com.czl.base.util.RxThreadHelper

class ActivityDetailViewModel(application: MyApplication, model: DataRepository) :
    BaseViewModel<DataRepository>(application, model) {
    var activityId=""
    var joinAble=ObservableField<Boolean>(false)
    var detailData= ObservableField<ActivityDetailBean>(ActivityDetailBean(ActivityDetailBean.ProActivity("",0,"","","","","","","",0.0,0,0,"","",0,"",""),0))
    val onRefreshListener: BindingCommand<Void> = BindingCommand(BindingAction {
        getAcitivityDetail()
    })
    val onRefreshFinishEvent: SingleLiveEvent<Boolean> = SingleLiveEvent()
    val toNoticeDetail:SingleLiveEvent<String> = SingleLiveEvent()
    var onClickSubmit: BindingCommand<Any> = BindingCommand(BindingAction {
        toNoticeDetail.postValue(activityId)
    })

//     fun joinActivity() {
//        model.joinActivity(activityId)
//            .compose(RxThreadHelper.rxSchedulerHelper(this))
//            .doOnSubscribe { showLoading() }
//            .subscribe(object : ApiSubscriber200Helper<BaseBean<Any?>>(){
//                override fun onResult(t: BaseBean<Any?>) {
//                    dismissLoading()
//                    onJoinActivityEvent.call()
//                }
//
//                override fun onFailed(msg: String?) {
//                    dismissLoading()
//                    showErrorToast(msg)
//                }
//
//            })
//    }

    private fun getAcitivityDetail() {
        model.getActivityDetail(activityId)
            .compose(RxThreadHelper.rxSchedulerHelper(this))
            .subscribe(object : ApiSubscriber200Helper<BaseBean<ActivityDetailBean>>(){
                override fun onResult(t: BaseBean<ActivityDetailBean>) {
                    if (t.data?.proActivity==null){
                        showNormalToast("无此活动！")
                        joinAble.set(false)
                    }else{
                        joinAble.set(true)
                        detailData.set(t.data)
                    }
                    onRefreshFinishEvent.postValue(true)
                }

                override fun onFailed(msg: String?) {
                    showErrorToast(msg)
                    onRefreshFinishEvent.postValue(false)
                }
            })
    }
}